package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    //定义一个长度为5的老师数组
    private static Teacher[] teachers = new Teacher[5];

    public boolean addTeacher(Teacher teacher) {
        //定义一个标记,-1表示数组对象满了
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            //遍历数组,拿到当个对象比较
            Teacher ter = teachers[i];
            //如果对象为空表示可以存放
            if (ter == null) {
                index = i;
                break;
            }
        }
        //最后判断标记位,等于-1表示满了,添加不上,返回false
        if (index == -1) {
            return false;
        }else{
            //否则把当前的对象记录到索引位置,返回tru表示成功添加
            teachers[index] = teacher;
            return true;
        }

    }
//返回所有对象数组
    public Teacher[] findAllTeacher() {
        return  teachers;
    }
//删除老师
    public void deletTeacher(String id) {
        int index = getIndex(id);
        teachers[index] = null;
    }
    //获取指定学号在索引中的位置
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher ter = teachers[i];
            if(ter!=null&&ter.getSid().equals(id)){
                index = i;
            }
        }
        return index;
    }

    //修改老师
    public void updateTeacher(String updateId,Teacher teacher) {
        int index = getIndex(updateId);
        teachers[index]=teacher;
    }
}
